package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.example.demo.entity.Sysuser;
import com.example.demo.mapper.DataMapper;
import com.example.demo.service.ISysuserService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {
    @Resource
    private DataMapper dataMapper;

    //使用分页查询增加service配置，继承的基础类比之mapper更多
    @Resource
    private ISysuserService iSysuserService;

    @RequestMapping("/list")
    public String list(Model model){
        return "list";
    }

    /**
     * 列表分页查询
     * @return
     */
    @RequestMapping("/listPage")   //使用注解方式定论方法路径
    @ResponseBody                  //post请求返回结果对象
    public Object listPage(@RequestParam Integer offset, @RequestParam Integer limit){
        //使用mybatis条件构造器进行分页查询
        EntityWrapper con = new EntityWrapper<Sysuser>();
//        Integer offset = 1; // 从第几条开始查询
//        Integer limit = 5;  //每页显示N条数据

        Page page = new Page(offset,limit); //注意不需要传实体类
        //通过service调用查询分页方法，可以返回Page对象,如果需要返回数据总数、分页数，选择selectMapsPage分页方法
        Page<Map<String, Object>> pageRes =iSysuserService.selectMapsPage(page,con);

        //查询总数
        page.getTotal();
        System.out.println(page.getTotal());
        //查询分页数
        page.getPages();
        System.out.println(page.getPages());

        return pageRes;
    }


    @RequestMapping("/listPage2")   //使用注解方式定论方法路径
    @ResponseBody                  //post请求返回结果对象
    public String listPage2(@RequestParam Integer offset, @RequestParam Integer limit){
        //使用mybatis条件构造器进行分页查询
        EntityWrapper con = new EntityWrapper<Sysuser>();


        Page page = new Page(offset,limit); //注意不需要传实体类
        //通过service调用查询分页方法，可以返回Page对象,如果需要返回数据总数、分页数，选择selectMapsPage分页方法
        Page<Map<String, Object>> pageRes =iSysuserService.selectMapsPage(page,con);

        //对象转json字符串
        String pageStr= JSON.toJSONString(pageRes);

        //return pageRes;
        return pageStr;
    }


    @RequestMapping("/main")
    public String main(Model model){
        EntityWrapper con=new EntityWrapper<Sysuser>();
        List<Sysuser> res=dataMapper.selectMaps(con);

        //往前台传数据
        model.addAttribute("userList",res);

        return "main";
    }



    @RequestMapping("/main2")
    public String main2(Model model){
        EntityWrapper con=new EntityWrapper<Sysuser>();
        List<Sysuser> res=dataMapper.selectMaps(con);

        model.addAttribute("userList",res);

        return "main2";
    }

    @RequestMapping("/")
    @ResponseBody
    public List<Map<String, Object>> Index(){
        List<Map<String,Object>> lists = dataMapper.
                selectbwbook();
        return lists;
    }

    @RequestMapping("/menu")
    @ResponseBody
    public String menu(){
        return "菜单";
    }

    @RequestMapping("/select")
    @ResponseBody
    public Object selectObj(){
        //定义条件构造器
        EntityWrapper con=new EntityWrapper<Sysuser>();
        List<Sysuser> res=dataMapper.selectMaps(con);

        return res;

    }

    @RequestMapping("/insert")
    @ResponseBody
    public int insertobj(){

        Sysuser data=new Sysuser();
        data.setId(3);
        data.setUsername("wangwu");
        data.setPassword("123");
        data.setName("王五");
        int res=dataMapper.insert(data);
        return res;
    }

    @RequestMapping("/check")
    @ResponseBody
    public int check(@RequestParam String username){

//        Sysuser data=new Sysuser();
//        data.setId(3);
//        data.setUsername("wangwu");
//        data.setPassword("123");
//        data.setName("王五");
//        int res=dataMapper.insert(data);
//        return res;

        EntityWrapper con = new EntityWrapper<Sysuser>();
        con.eq("username",username);
        List<Sysuser> res = dataMapper.selectMaps(con);
        if(res.isEmpty()){
            return 0;
        }
        return 1;
    }


    @RequestMapping("/delete")
    @ResponseBody
    public int deleteobj(){

        EntityWrapper con=new EntityWrapper<Sysuser>();
        con.eq("id",1);
        int res=dataMapper.delete(con);
        return res;
    }

    @RequestMapping("/update")
    @ResponseBody
    public int updateobj(){

        EntityWrapper con=new EntityWrapper<Sysuser>();
        con.eq("id",2);

        Sysuser data=new Sysuser();
        data.setId(2);
        data.setUsername("liuneng");
        data.setPassword("123");
        data.setName("赵六");
        int res=dataMapper.update(data,con);
        return res;
    }
}
